#!/usr/bin/perl

use strict;

my @months = ( "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
	       "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" );

sub genseason() {
    my %season = (
	'condition' => [],
	'temperature' => [],
	'pressure' => [],
	'humidity' => []
	);
    return %season;
}

my %monthdata;
for (my $i = 0; $i <= $#months; $i++) {
    my %s = &genseason();
    $monthdata{$months[$i]} = \%s;
}

my @seasons = ( "summer", "autumn", "winter", "spring", "aprsem", "octsem", "year" );
my @seasonmonths = ( [ "DEC", "JAN", "FEB" ],
		     [ "MAR", "APR", "MAY" ],
		     [ "JUN", "JUL", "AUG" ],
		     [ "SEP", "OCT", "NOV" ],
		     [ "APR", "MAY", "JUN", "JUL", "AUG", "SEP" ],
		     [ "OCT", "NOV", "DEC", "JAN", "FEB", "MAR" ],
		     [ "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
		       "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" ] );

my %seasondata;
for (my $i = 0; $i <= $#seasons; $i++) {
    my %s = &genseason();
    $seasondata{$seasons[$i]} = \%s;
}

for (my $i = 0; $i <= $#months; $i++) {
    my $f = "atmostemp_".$months[$i]."_100000.txt.good";
    my @l = &readfile($f);
    my $tf = "mediantemperature_".$months[$i].".txt.good";
    my @tl = &readfile($tf);
    my $pf = "medianpressure_".$months[$i].".txt.good";
    my @pl = &readfile($pf);
    my $hf = "medianhumidity_".$months[$i].".txt.good";
    my @hl = &readfile($hf);

    &adddata(\@l, \@tl, \@pl, \@hl, $monthdata{$months[$i]});

    &analyse($months[$i], \@l, \@tl, \@pl, \@hl);
}

for (my $i = 0; $i <= $#seasons; $i++) {
    my $sr = $seasondata{$seasons[$i]};
    for (my $j = 0; $j <= $#{$seasonmonths[$i]}; $j++) {
	my $dr = $monthdata{$seasonmonths[$i]->[$j]};
	&adddata($dr->{'condition'}, $dr->{'temperature'}, $dr->{'pressure'},
		 $dr->{'humidity'}, $sr);
    }
    &analyse($seasons[$i], $sr->{'condition'}, $sr->{'temperature'},
	     $sr->{'pressure'}, $sr->{'humidity'});
}

sub analyse($$$$) {
    my $n = shift;
    my $c = shift;
    my $t = shift;
    my $p = shift;
    my $h = shift;

    print "\n===========\n";
    print "In ".$n.":\n";

    my @sl = sort { $a->[1] <=> $b->[1] } @{$c};
    &printcondition("best", \@sl, $t, $p, $h, 0, 0);
    &printcondition("worst", \@sl, $t, $p, $h, $#sl, $#sl);

    my $n = $#sl + 1;
    my ($m, $o);
    if ($n % 2) {
	$m = $#sl / 2;
	$o = $m;
    } else {
	$m = $n / 2;
	$o = $m - 1;
    }
    &printcondition("median", \@sl, $t, $p, $h, $m, $o);
}

sub printcondition($$$$$$$) {
    my $n = shift;
    my $sl = shift;
    my $t = shift;
    my $p = shift;
    my $h = shift;
    my $o = shift;
    my $m = shift;
    
    my $d = ($sl->[$m]->[0] + $sl->[$o]->[0]) / 2;
    my $v = ($sl->[$m]->[1] + $sl->[$o]->[1]) / 2;
    printf "time with %s condition: %.4f with %.3f\n", $n, $d, $v;

    my $tl = &arrwhere($t, $d);
    my $pl = &arrwhere($p, $d);
    my $hl = &arrwhere($h, $d);
    printf "  this is T = %.1f P = %.1f H = %.1f\n", $t->[$tl]->[1], ($p->[$pl]->[1] / 100), $h->[$hl]->[1];
}

sub readfile($) {
    my $f = shift;
    open(A, $f);
    my @l;
    while(<A>) {
	chomp;
	my @els = split(/\s+/);
	push @l, \@els;
    }
    close(A);

    return @l;
}

sub arrwhere($$) {
    my $arrref = shift;
    my $v = shift;

    my ($d, $p);
    for (my $i = 0; $i <= $#{$arrref}; $i++) {
	my $dv = abs($arrref->[$i]->[0] - $v);
	if ($i == 0) {
	    $d = $dv;
	    $p = 0;
	} elsif ($dv < $d) {
	    $d = $dv;
	    $p = $i;
	}
    }

    return $p;
}

sub adddata($$$$$) {
    my $c = shift;
    my $t = shift;
    my $p = shift;
    my $h = shift;
    my $dr = shift;

    my $lv = 0;
    my $dcl = $#{$dr->{'condition'}};
    if ($dcl >= 0) {
	$lv = $dr->{'condition'}->[$dcl]->[0];
    }
    for (my $i = 0; $i <= $#{$c}; $i++) {
	push @{$dr->{'condition'}}, [ ($c->[$i]->[0] + $lv), $c->[$i]->[1] ];
	push @{$dr->{'temperature'}}, [ ($t->[$i]->[0] + $lv), $t->[$i]->[1] ];
	push @{$dr->{'pressure'}}, [ ($p->[$i]->[0] + $lv), $p->[$i]->[1] ];
	push @{$dr->{'humidity'}}, [ ($h->[$i]->[0] + $lv), $h->[$i]->[1] ];
    }
}
